package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @GetMapping("getUsername")
    public Result getUsername(){
        // security的上下文，security的所有信息
        User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
    }

    @GetMapping("/getMenu")
    public Result getMenu(){
        org.springframework.security.core.userdetails.User user=
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Menu> list=userService.getMenu(user.getUsername());
        return new Result(true,MessageConstant.GET_MENU_SUCCESS,list);
    }


    //分页查询记录
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<com.itheima.pojo.User> pageList = null;
        try {
            pageList = userService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_USER_SUCCESS,pageList);
    }


    //新增记录
    @RequestMapping("/add")
    public Result add(Integer[] roleIds, @RequestBody com.itheima.pojo.User user){
        try {
            userService.add(roleIds,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    //回显编辑数据
    @RequestMapping("/findById")
    public Result findById(int id){
        com.itheima.pojo.User user = userService.findById(id);
        return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
    }

    //编辑角色
    @RequestMapping("/edit")
    public Result edit(Integer[] roleIds, @RequestBody com.itheima.pojo.User user){
        try {
            userService.edit(roleIds,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    //根据id删除记录
    @RequestMapping("/deleteById")
    public Result deleteById(int id){
        userService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
    }

}
